package com.jjwww.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjwww.common.QueryPageParam;
import com.jjwww.common.Result;
import com.jjwww.entity.Books;
import com.jjwww.service.impl.BooksServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjwww
 * @since 2025-10-23
 */
@RestController
@RequestMapping("/books")
public class BooksController {
    @Autowired
    public BooksServiceImpl booksService;

    @GetMapping ("/list1")
    public List<Books> list1() {
        return booksService.list();
    }
    //编辑按钮中的新增
    @PostMapping("/save")
    public Result save(@RequestBody Books books){
        return booksService.save(books)?Result.suc():Result.fail();
    }
    //编辑按钮中的更新
    @PostMapping("/update")
    public Result update(@RequestBody Books books){
        return booksService.updateById(books)?Result.suc():Result.fail();
    }
    //编辑按钮中的删除
    @GetMapping ("/del")
    public Result del(@RequestParam String id){
        return booksService.removeById(id)?Result.suc():Result.fail();
    }
    //查询
    @PostMapping("/listPage")
    public Result  listPage(@RequestBody QueryPageParam query){

        HashMap param = query.getParam();
        String bookname = (String)param.get("bookname");
        String bookuid = (String)param.get("bookuid");

        //总记录数，当前页
        Page<Books> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Books> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //通过选择书名来查询
        if (StringUtils.isNotBlank(bookname) && !"null".equals(bookname)){
            lambdaQueryWrapper.like(Books::getBookname,bookname);
        }
        //通过选择小说类目来查询
        if (StringUtils.isNotBlank(bookuid) && !"null".equals(bookuid)){
            lambdaQueryWrapper.eq(Books::getBookuid,bookuid);
        }

        IPage result = booksService.pageBB(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }
    //加载数据
    @GetMapping("/list")
    public Result list(){
        List list = booksService.list();
        return Result.suc(list);
    }







    //新增
    @PostMapping("/saveb")
    public boolean saveb(@RequestBody Books books){
        return booksService.save(books);
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Books books) {
        return booksService.updateById(books);
    }
    //新增或修改
    @PostMapping("/saveOrmod")
    public boolean saveOrmod(@RequestBody Books books){
        return booksService.saveOrUpdate(books);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return booksService.removeById(id);
    }
    //查询
    @PostMapping("/listP")
    public List<Books> listp(@RequestBody Books books){
        LambdaQueryWrapper<Books> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Books::getBookname,books.getBookname());
        return booksService.list(lambdaQueryWrapper);
    }

}
